package com.ruoyi.swla2.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.ruoyi.swla2.domain.Zone;
import com.ruoyi.swla2.service.IZoneService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.swla2.domain.BiZbx;
import com.ruoyi.swla2.service.IBiZbxService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * BI指标值Controller
 *
 * @author gdqt
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/swla2/biZbx")
public class BiZbxController extends BaseController {
    @Autowired
    private IBiZbxService biZbxService;

    @Autowired
    private IZoneService zoneService;

    /**
     * 查询BI指标值列表
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/getZCOverviewData")
    public AjaxResult getZCOverviewData(BiZbx biZbx) {
        Map<String, Object> resultMap = new HashMap<>();

        biZbx.setZbfl("bi_dfq_gsl");
        biZbx.setZbz3(LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN)));
        List<BiZbx> lastMonthZbxList = biZbxService.selectBiZbxList(biZbx);
        if (CollUtil.isNotEmpty(lastMonthZbxList)) {
            BiZbx lastMontZbx = lastMonthZbxList.get(0);
            resultMap.put("supply", lastMontZbx.getZbz4());
            resultMap.put("supplyHb", lastMontZbx.getZbz5());
            resultMap.put("supplyTb", lastMontZbx.getZbz6());
            resultMap.put("sale", lastMontZbx.getZbz5());
            resultMap.put("saleHb", lastMontZbx.getZbz7());
            resultMap.put("saleTb", lastMontZbx.getZbz8());
            resultMap.put("free", lastMontZbx.getZbz6());
            resultMap.put("freeHb", lastMontZbx.getZbz9());
            resultMap.put("freeTb", lastMontZbx.getZbz10());
            resultMap.put("cxcl", lastMontZbx.getZbz7());
            resultMap.put("cxclHb", lastMontZbx.getZbz11());
            resultMap.put("cxclTb", lastMontZbx.getZbz12());
            resultMap.put("dyhYsl", lastMontZbx.getZbz13());
            resultMap.put("dyhYslHb", lastMontZbx.getZbz14());
        }

        return success(resultMap);
    }

    /**
     * 查询BI指标值列表
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/getOverviewData")
    public AjaxResult getOverviewData(BiZbx biZbx) {
        Map<String, Object> resultMap = new HashMap<>();

        biZbx.setZbfl("bi_qnzbzl");
        biZbx.setZbz(LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_YEAR_PATTERN)));
        List<BiZbx> annualZbxList = biZbxService.selectBiZbxList(biZbx);
        if (CollUtil.isNotEmpty(annualZbxList)) {
            BiZbx lastMontZbx = annualZbxList.get(0);
            resultMap.put("supplyQn", lastMontZbx.getZbz1());
            resultMap.put("saleQn", lastMontZbx.getZbz2());
            resultMap.put("freeQn", lastMontZbx.getZbz3());
            resultMap.put("cxclQn", lastMontZbx.getZbz4());
        }

        biZbx.setZbfl("bi_syzbdb");
        biZbx.setZbz(LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN)));
        List<BiZbx> lastMonthZbxList = biZbxService.selectBiZbxList(biZbx);
        if (CollUtil.isNotEmpty(lastMonthZbxList)) {
            BiZbx lastMontZbx = lastMonthZbxList.get(0);
            resultMap.put("supply", lastMontZbx.getZbz1());
            resultMap.put("supplyHb", lastMontZbx.getZbz5());
            resultMap.put("supplyTb", lastMontZbx.getZbz6());
            resultMap.put("sale", lastMontZbx.getZbz2());
            resultMap.put("saleHb", lastMontZbx.getZbz7());
            resultMap.put("saleTb", lastMontZbx.getZbz8());
            resultMap.put("free", lastMontZbx.getZbz3());
            resultMap.put("freeHb", lastMontZbx.getZbz9());
            resultMap.put("freeTb", lastMontZbx.getZbz10());
            resultMap.put("cxcl", lastMontZbx.getZbz4());
            resultMap.put("cxclHb", lastMontZbx.getZbz11());
            resultMap.put("cxclTb", lastMontZbx.getZbz12());
            resultMap.put("dyhYsl", lastMontZbx.getZbz13());
            resultMap.put("dyhYslHb", lastMontZbx.getZbz14());
        }

        biZbx.setZbfl("bi_sjtj");
        biZbx.setZbz(null);
        List<BiZbx> sjtjZbxList = biZbxService.selectBiZbxList(biZbx);
        if (CollUtil.isNotEmpty(sjtjZbxList)) {
            BiZbx sjtjZbx = sjtjZbxList.get(0);
            resultMap.put("dfq", sjtjZbx.getZbz4());
            resultMap.put("dma", sjtjZbx.getZbz1());
            resultMap.put("dyh", sjtjZbx.getZbz2());
            resultMap.put("meter", sjtjZbx.getZbz3());
        }

        return success(resultMap);
    }

    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/getDfqKpiData")
    public AjaxResult getDfqKpiData(BiZbx biZbx) {
        Map<String, Object> resultMap = new HashMap<>();

        // 公司上月供水
        biZbx.setZbfl("bi_syzbdb");
        biZbx.setZbz(LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN)));
        List<BiZbx> companyDataList = biZbxService.selectBiZbxList(biZbx);
        if (!companyDataList.isEmpty()) {
            resultMap.put("company", companyDataList.get(0));
        }
        biZbx.setZbz(LocalDate.now().minusMonths(12L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN)) + "-01");
        resultMap.put("company_all", biZbxService.selectDfqKpiByZbz(biZbx));

        // 最近12个月的供水量数据
        biZbx.setZbz3(LocalDate.now().minusMonths(12L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN)) + "-01");
        List<BiZbx> dfqKpiZbxList = biZbxService.selectDfqKpiByZbz3(biZbx);
        resultMap.put("all", dfqKpiZbxList);
        // 最近12个月的供水量数据 按每个月分组
        resultMap.put("group", dfqKpiZbxList.stream().collect(Collectors.groupingBy(BiZbx::getZbz3)));

        // 上一个月的供水量数据
        biZbx.setZbfl("bi_dfq_gsl");
        biZbx.setZbz(null);
        biZbx.setZbz3(LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN)));
        List<BiZbx> lastMonthKpiZbxList = biZbxService.selectBiZbxList(biZbx);
        resultMap.put("kpi", lastMonthKpiZbxList);

        // 大分区基础信息数据
        Zone zone = new Zone();
        zone.setLevelType("1");
        List<Zone> zoneList = zoneService.selectZoneList(zone);
        resultMap.put("dfq", zoneList);

        return success(resultMap);
    }

    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/getDMAKpiData")
    public AjaxResult getDMAKpiData(BiZbx biZbx) {
        Map<String, Object> resultMap = new HashMap<>();

        biZbx.setZbfl("bi_dma_gsl");
        biZbx.setZbz3(LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN)));
        List<BiZbx> kpiZbxList = biZbxService.selectBiZbxList(biZbx);
        resultMap.put("kpi", kpiZbxList);

        Zone zone = new Zone();
        zone.setLevelType("2");
        if (StrUtil.isNotEmpty(biZbx.getZbz2())) {
            zone.setPID(biZbx.getZbz2());
        }
        List<Zone> zoneList = zoneService.selectZoneList(zone);
        resultMap.put("dma", zoneList);

        return success(resultMap);
    }

    /**
     * 查询BI指标值列表
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/list")
    public TableDataInfo list(BiZbx biZbx) {
        startPage();
        List<BiZbx> list = biZbxService.selectBiZbxList(biZbx);
        return getDataTable(list);
    }

    /**
     * 导出BI指标值列表
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:export')")
    @Log(title = "BI指标值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BiZbx biZbx) {
        List<BiZbx> list = biZbxService.selectBiZbxList(biZbx);
        ExcelUtil<BiZbx> util = new ExcelUtil<BiZbx>(BiZbx.class);
        util.exportExcel(response, list, "BI指标值数据");
    }

    /**
     * 获取BI指标值详细信息
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(biZbxService.selectBiZbxByID(ID));
    }

    /**
     * 新增BI指标值
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:add')")
    @Log(title = "BI指标值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BiZbx biZbx) {
        biZbx.setTenantId(this.getTenantId());
        return toAjax(biZbxService.insertBiZbx(biZbx));
    }

    /**
     * 修改BI指标值
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:edit')")
    @Log(title = "BI指标值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BiZbx biZbx) {
        return toAjax(biZbxService.updateBiZbx(biZbx));
    }

    /**
     * 删除BI指标值
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:remove')")
    @Log(title = "BI指标值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(biZbxService.deleteBiZbxByIDs(IDs));
    }
}
